#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
lambda : Command ('Move', creature)
	or
lambda : Command.Move(creature)

Tiles can be pre-initialised to dist = 99, then at end of use, the
contents of visited[] can be reset.

Or they can be initialised everytime right after this comment.

Still left to define lots of Tile (or Point, or whatever) member
functions: visited_ and non_visited_neighbours, get_modifier, and
unoccupied, occupied_by_enemy and make_occupiable.
'''

unit_type = creature.unit_type
s = creature.position
s.dist = 0
visited = [s]
Q = [s]
while len(Q):
	tile = Q.pop(0)
	for n in (set(tile.neighbours()) - set(visited)):
		if n.occupied_by_enemy(): continue
		for v in (set(n.neighbours()) & set(visited)):
			alt = n.get_modifier(unit_type) + v.dist
			if alt < n.dist:
				n.dist = alt
				n.previous = v
		visited.append(n)
		if n.dist < creature.MV and n not in Q:
			Q.append(n)
			if n.unoccupied(): n.make_occupiable()
